package com.hxkj.controller;


import com.github.pagehelper.PageInfo;
import com.hxkj.domain.po.DictType;
import com.hxkj.service.DictTypeService;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * 字典分类控制器
 * <p>
 * Created by zzx on 2018/5/15.
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 字典分类列表
     *
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getlist", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultDataStruct<PageInfo> getDictItem(@RequestBody ParameterDataStruct<DictType> parameterDataStruct) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            PageInfo pageInfo = dictTypeService.getDictTypeList(parameterDataStruct.getData(), parameterDataStruct.getPageTo());
            resultDataStruct.setCode(200);
            resultDataStruct.setData(pageInfo);
        } catch (Exception e) {
            resultDataStruct.setCode(400);
            resultDataStruct.setData(e.getMessage());
        }
        return resultDataStruct;


    }

    /**
     * 获取一个字典信息  一 级（主类）
     *
     * @param dictType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<DictType>> getDictItem(@RequestBody DictType dictType) throws Exception {
        return () -> dictTypeService.getDictTypeFromCache(dictType);
    }

    /**
     * 新增字典     一 级（主类）
     *
     * @param dictType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> saveDictItem(@RequestBody DictType dictType) throws Exception {
        return () -> dictTypeService.saveDictType(dictType);

    }

    /**
     * 删除一个字典信息   一 级（主类）
     *
     * @param dictType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> deleteDictItem(@RequestBody DictType dictType) throws Exception {
        return () -> dictTypeService.deleteDictType(dictType);
    }

    /**
     * 修改一个字典信息   一 级（主类）
     *
     * @param dictType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> updateDictItem(@RequestBody DictType dictType) throws Exception {
        return () -> dictTypeService.updateDictType(dictType);
    }

    /**
     * 禁用一个 一级 字典
     *
     * @param dictType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifyDictType(@RequestBody DictType dictType) throws Exception {
        return () -> dictTypeService.modifyDictType(dictType);
    }

    @RequestMapping(value = "/getDictType")
    public Callable<ResultDataStruct<DictType>> getDictType(@RequestBody DictType dictType) throws Exception{
        return () -> dictTypeService.getDictType(dictType);
    }

}